package com.sprouting.personal.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.personal.model.common.ResultJson;
import com.sprouting.personal.model.common.jsr.AddGroup;
import com.sprouting.personal.model.common.jsr.UpdGroup;
import com.sprouting.personal.model.entity.DynamicDataDetailed;
import com.sprouting.personal.model.entity.DynamicRecording;
import com.sprouting.personal.service.DynamicDataDetailedService;
import com.sprouting.personal.service.DynamicRecordingService;
import com.sprouting.personal.utils.CheckTool;
import com.sprouting.personal.utils.EntityTool;
import com.sprouting.personal.utils.IdTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 动态数据表
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/1/30 10:39
 */
@RequestMapping("/dynamic")
@RestController
public class DynamicRecordingController {

    @Autowired
    private DynamicRecordingService dynamicRecordingService;
    @Autowired
    private DynamicDataDetailedService dynamicDataDetailedService;


    /**
     * 分页查询动态数据表
     * @param page 分页参数
     * @param dynamicRecording 查询参数
     * @return
     */
    @PostMapping("/getDynamicRecordingListPage")
    public ResultJson getDynamicRecordingListPage(Page page, DynamicRecording dynamicRecording){
        if (dynamicRecording == null){
            dynamicRecording = new DynamicRecording();
        }
        dynamicRecording.setUserId(StpUtil.getLoginIdAsString());
        return ResultJson.success(dynamicRecordingService.getDynamicRecordingListPage(page, dynamicRecording));
    }

    /**
     * 保存动态主表数据
     * @param dynamicRecording 新建一个名录
     * @return
     */
    @PostMapping("/saveDynamicRecording")
    public ResultJson saveDynamicRecording(@Validated(AddGroup.class) DynamicRecording dynamicRecording){
        String msg = checkDynamicRecording(dynamicRecording);
        if (msg != null){
            return ResultJson.error(msg);
        }

        dynamicRecording.setId(IdTool.getId());
        dynamicRecording.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.buildSaveBase(dynamicRecording, StpUtil.getLoginIdAsString());
        dynamicRecordingService.save(dynamicRecording);
        return ResultJson.success(dynamicRecording);
    }

    /**
     * 更新动态主表数据
     * @param dynamicRecording 新建一个名录
     * @return
     */
    @PostMapping("/updDynamicRecording")
    public ResultJson updDynamicRecording(@Validated(UpdGroup.class) DynamicRecording dynamicRecording){
        String msg = checkDynamicRecording(dynamicRecording);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.buildUpdBase(dynamicRecording, StpUtil.getLoginIdAsString());
        dynamicRecordingService.updateById(dynamicRecording);
        return ResultJson.success();
    }

    /**
     * 删除动态数据
     * @param id 动态数据主表ID
     * @return
     */
    @PostMapping("/delDynamicRecordingById")
    public ResultJson delDynamicRecordingById(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorIllegal();
        }
        dynamicRecordingService.delDynamicRecordingById(id);
        return ResultJson.success();
    }


    /*
        动态数据明细表***************************
        动态数据明细表***************************
        动态数据明细表***************************
    * */


    /**
     * 保存动态明细
     * @param dynamicDataDetailed 动态明细
     * @return
     */
    @PostMapping("/saveDynamicDataDetailed")
    public ResultJson saveDynamicDataDetailed(DynamicDataDetailed dynamicDataDetailed){
        String msg = checkDynamicDataDetailed(dynamicDataDetailed);
        if (msg != null){
            return ResultJson.error(msg);
        }

        dynamicDataDetailed.setId(IdTool.getId());
        dynamicDataDetailed.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.buildSaveBase(dynamicDataDetailed, StpUtil.getLoginIdAsString());
        dynamicDataDetailedService.save(dynamicDataDetailed);
        return ResultJson.success();
    }

    /**
     * 更新动态明细
     * @param dynamicDataDetailed 动态明细
     * @return
     */
    @PostMapping("/updDynamicDataDetailed")
    public ResultJson updDynamicDataDetailed(DynamicDataDetailed dynamicDataDetailed){
        String msg = checkDynamicDataDetailed(dynamicDataDetailed);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.buildUpdBase(dynamicDataDetailed, StpUtil.getLoginIdAsString());
        dynamicDataDetailedService.updateById(dynamicDataDetailed);
        return ResultJson.success();
    }

    /**
     * 物理删除动态明细
     * @param id 明细ID
     * @return
     */
    @PostMapping("/delDynamicDataDetailedById")
    public ResultJson delDynamicDataDetailedById(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorIllegal();
        }
        dynamicDataDetailedService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 分页查询全部的数据
     * @param page 分页参数
     * @param dynamicDataDetailed 查询参数
     * @return
     */
    @PostMapping("/getDynamicDataDetailedListPage")
    public ResultJson getDynamicDataDetailedListPage(Page page, DynamicDataDetailed dynamicDataDetailed){
        if (dynamicDataDetailed == null){
            dynamicDataDetailed = new DynamicDataDetailed();
        }
        if (CheckTool.checkNull(dynamicDataDetailed.getDynamicId())){
            return ResultJson.error("动态主表参数异常");
        }
        dynamicDataDetailed.setUserId(StpUtil.getLoginIdAsString());
        return ResultJson.success(dynamicDataDetailedService.getDynamicDataDetailedListPage(page, dynamicDataDetailed));
    }

    /**
     * 获取折线图的需要的数据
     * @param dynamicId 动态主表ID
     * @return
     */
    @PostMapping("/getLineChartData")
    public ResultJson getLineChartData(String dynamicId){
        if (CheckTool.checkNull(dynamicId)){
            return ResultJson.errorIllegal();
        }
        DynamicRecording recording = dynamicRecordingService.getRecordingById(dynamicId);
        if (recording == null){
            return ResultJson.errorPram();
        }

        Page page = new Page(1, recording.getTop());
        DynamicDataDetailed dynamicDataDetailed = new DynamicDataDetailed();
        dynamicDataDetailed.setUserId(StpUtil.getLoginIdAsString());
        dynamicDataDetailed.setDynamicId(dynamicId);
        // 获取最新的
        Page<DynamicDataDetailed> detailedPage = dynamicDataDetailedService.getDynamicDataDetailedListPage(page, dynamicDataDetailed);
        List<DynamicDataDetailed> records = detailedPage.getRecords();
        // 要进行一下倒序，否则数据不太好看
        Collections.reverse(records);
        return ResultJson.success(records);
    }

    /**
     * 校验检查动态数据主表
     * @param dynamicRecording 动态数据主表
     * @return
     */
    private String checkDynamicRecording(DynamicRecording dynamicRecording){
        if (dynamicRecording.getTop() == null){
            // 默认展示20条数据
            dynamicRecording.setTop(20);
        }
        if (dynamicRecording.getTop() <= 0){
            // 自动处理
            dynamicRecording.setTop(20);
        }

        return null;
    }

    /**
     * 校验动态明细，经过测试，发现注解权限无法解决 BigDecimal 和 date 的注入，不进行研究了，直接这里校验
     * @param dynamicDataDetailed 动态明细
     * @return
     */
    private String checkDynamicDataDetailed(DynamicDataDetailed dynamicDataDetailed){
        if (dynamicDataDetailed == null){
            return "无效数据";
        }
        if (dynamicDataDetailed.getEntryTime() == null){
            return "数据时间不能为空";
        }
        if (dynamicDataDetailed.getValue() == null){
            return "数值不能为空";
        }
        if (CheckTool.checkNull(dynamicDataDetailed.getDynamicId())){
            return "必须隶属一张数据表";
        }
        DynamicDataDetailed db = dynamicDataDetailedService.getDynamicDataDetailedByEntryTime(dynamicDataDetailed.getId(), StpUtil.getLoginIdAsString(), dynamicDataDetailed.getEntryTime(), dynamicDataDetailed.getDynamicId());
        if (db != null){
            return "该时间已存在对应数据，请勿重复添加";
        }
        return null;
    }
}
